<template>
  <div class="app-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>智慧档案</el-breadcrumb-item>
        <el-breadcrumb-item>空气质量监控</el-breadcrumb-item>
      </el-breadcrumb>
      <div class="header-actions">
        <el-button type="primary" icon="el-icon-refresh" @click="refreshData">刷新数据</el-button>
        <el-button type="success" icon="el-icon-setting">参数配置</el-button>
      </div>
    </div>

    <!-- 设备选择区域 -->
    <div class="device-selector">
      <el-card shadow="never">
        <div class="selector-content">
          <div class="selector-item">
            <span class="label">A库房</span>
            <el-select v-model="selectedDevice" placeholder="区域控制器-空气质量..." @change="onDeviceChange">
              <el-option label="区域控制器-空气质量监测" value="device1"></el-option>
              <el-option label="区域控制器-云测仪" value="device2"></el-option>
            </el-select>
          </div>
          <div class="device-tabs">
            <el-tabs v-model="activeTab" @tab-click="handleTabClick">
              <el-tab-pane label="云测仪" name="sensor">
                <el-button type="text" icon="el-icon-view">云测仪</el-button>
              </el-tab-pane>
              <el-tab-pane label="灯光设备" name="lighting">
                <el-button type="text" icon="el-icon-sunny">灯光设备</el-button>
              </el-tab-pane>
              <el-tab-pane label="防火预警" name="fire">
                <el-button type="text" icon="el-icon-warning-outline">防火预警</el-button>
              </el-tab-pane>
            </el-tabs>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <el-row :gutter="20">
        <!-- 左侧内容 -->
        <el-col :span="16">
          <!-- 空气质量指标卡片 -->
          <div class="air-quality-cards">
            <el-row :gutter="16">
              <el-col :span="6" v-for="(indicator, index) in airQualityIndicators" :key="index">
                <el-card class="air-quality-card" :class="indicator.type + '-card'">
                  <div class="card-header">
                    <i :class="indicator.icon"></i>
                    <span>{{ indicator.name }}</span>
                  </div>
                  <div class="card-content">
                    <div class="value">{{ indicator.value }}</div>
                    <div class="unit">{{ indicator.unit }}</div>
                    <div class="status" :class="indicator.status">{{ getStatusText(indicator.status) }}</div>
                  </div>
                </el-card>
              </el-col>
            </el-row>
          </div>

          <!-- 空气质量曲线图 -->
          <el-card class="chart-card" style="margin-top: 20px;">
            <div slot="header" class="chart-header">
              <span>空气质量曲线</span>
              <div class="chart-legend">
                <span class="legend-item tvoc">
                  <i class="legend-dot"></i>TVOC
                </span>
                <span class="legend-item pm25">
                  <i class="legend-dot"></i>PM2.5
                </span>
              </div>
            </div>
            <div id="airQualityChart" style="height: 300px;"></div>
          </el-card>
        </el-col>

        <!-- 右侧内容 -->
        <el-col :span="8">
          <!-- 设备状态 -->
          <el-card class="status-card">
            <div slot="header">
              <span>设备状态</span>
            </div>
            <div class="status-indicators">
              <div class="status-item" v-for="(indicator, index) in airQualityIndicators" :key="index">
                <div class="status-circle" :class="indicator.type + '-status'">
                  <i :class="indicator.icon"></i>
                </div>
                <div class="status-info">
                  <div class="status-value">{{ indicator.value }}{{ indicator.unit }}</div>
                  <div class="status-label">{{ indicator.name }}</div>
                  <div class="status-level" :class="indicator.status">{{ getStatusText(indicator.status) }}</div>
                </div>
              </div>
            </div>
          </el-card>

          <!-- 运行日志 -->
          <el-card class="log-card" style="margin-top: 20px;">
            <div slot="header">
              <span>运行日志</span>
            </div>
            <div class="log-content">
              <div class="log-header">
                <span>序号</span>
                <span>时间</span>
                <span>日志内容</span>
              </div>
              <div class="log-list">
                <div 
                  v-for="(log, index) in operationLogs" 
                  :key="index" 
                  class="log-item"
                >
                  <span class="log-index">{{ index + 1 }}</span>
                  <span class="log-time">{{ log.time }}</span>
                  <span class="log-content-text">{{ log.content }}</span>
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'AirQualityMonitor',
  data() {
    return {
      selectedDevice: 'device1',
      activeTab: 'sensor',
      airQualityIndicators: [
        {
          name: 'CO2',
          value: 0,
          unit: 'ppm',
          type: 'co2',
          icon: 'el-icon-cloudy-and-sunny',
          status: 'normal'
        },
        {
          name: 'O3',
          value: 0,
          unit: 'μg/m³',
          type: 'o3',
          icon: 'el-icon-sunny',
          status: 'normal'
        },
        {
          name: 'PM10',
          value: 0,
          unit: 'μg/m³',
          type: 'pm10',
          icon: 'el-icon-heavy-rain',
          status: 'normal'
        },
        {
          name: 'TVOC',
          value: 0,
          unit: 'mg/m³',
          type: 'tvoc',
          icon: 'el-icon-wind-power',
          status: 'normal'
        }
      ],
      operationLogs: [
        {
          time: '2025-05-21 14:43:23',
          content: 'TVOC浓度超标预警'
        },
        {
          time: '2025-05-21 09:19:52',
          content: '空气质量监测正常'
        },
        {
          time: '2025-05-21 08:30:15',
          content: 'PM2.5浓度偏高'
        }
      ],
      chart: null,
      chartData: {
        times: [],
        tvocData: [],
        pm25Data: []
      }
    }
  },
  mounted() {
    this.initChart()
    this.generateMockData()
    this.startDataUpdate()
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose()
    }
    if (this.timer) {
      clearInterval(this.timer)
    }
  },
  methods: {
    onDeviceChange(value) {
      console.log('设备切换:', value)
      this.refreshData()
    },
    handleTabClick(tab) {
      console.log('标签切换:', tab.name)
    },
    refreshData() {
      // 模拟数据刷新
      this.airQualityIndicators.forEach(indicator => {
        switch(indicator.type) {
          case 'co2':
            indicator.value = Math.floor(Math.random() * 1000) + 400
            indicator.status = indicator.value > 800 ? 'warning' : 'normal'
            break
          case 'o3':
            indicator.value = Math.floor(Math.random() * 100) + 20
            indicator.status = indicator.value > 80 ? 'warning' : 'normal'
            break
          case 'pm10':
            indicator.value = Math.floor(Math.random() * 50) + 10
            indicator.status = indicator.value > 40 ? 'warning' : 'normal'
            break
          case 'tvoc':
            indicator.value = (Math.random() * 2 + 0.1).toFixed(1)
            indicator.status = indicator.value > 1.5 ? 'warning' : 'normal'
            break
        }
      })
      this.updateChart()
    },
    getStatusText(status) {
      const statusMap = {
        normal: '正常',
        warning: '预警',
        danger: '危险'
      }
      return statusMap[status] || '未知'
    },
    initChart() {
      this.chart = echarts.init(document.getElementById('airQualityChart'))
      
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: this.chartData.times,
          axisLine: {
            lineStyle: {
              color: '#E4E7ED'
            }
          },
          axisLabel: {
            color: '#606266'
          }
        },
        yAxis: [
          {
            type: 'value',
            name: 'TVOC(mg/m³)',
            position: 'left',
            axisLine: {
              lineStyle: {
                color: '#E4E7ED'
              }
            },
            axisLabel: {
              color: '#606266'
            }
          },
          {
            type: 'value',
            name: 'PM2.5(μg/m³)',
            position: 'right',
            axisLine: {
              lineStyle: {
                color: '#E4E7ED'
              }
            },
            axisLabel: {
              color: '#606266'
            }
          }
        ],
        series: [
          {
            name: 'TVOC',
            type: 'line',
            yAxisIndex: 0,
            data: this.chartData.tvocData,
            smooth: true,
            lineStyle: {
              color: '#67C23A'
            },
            itemStyle: {
              color: '#67C23A'
            }
          },
          {
            name: 'PM2.5',
            type: 'line',
            yAxisIndex: 1,
            data: this.chartData.pm25Data,
            smooth: true,
            lineStyle: {
              color: '#E6A23C'
            },
            itemStyle: {
              color: '#E6A23C'
            }
          }
        ]
      }
      
      this.chart.setOption(option)
    },
    generateMockData() {
      const now = new Date()
      for (let i = 23; i >= 0; i--) {
        const time = new Date(now.getTime() - i * 60 * 60 * 1000)
        this.chartData.times.push(time.getHours().toString().padStart(2, '0'))
        this.chartData.tvocData.push((Math.random() * 2 + 0.5).toFixed(1))
        this.chartData.pm25Data.push(Math.floor(Math.random() * 30) + 10)
      }
      
      // 初始化指标数据
      this.refreshData()
    },
    updateChart() {
      if (this.chart) {
        this.chart.setOption({
          xAxis: {
            data: this.chartData.times
          },
          series: [
            {
              data: this.chartData.tvocData
            },
            {
              data: this.chartData.pm25Data
            }
          ]
        })
      }
    },
    startDataUpdate() {
      this.timer = setInterval(() => {
        // 模拟实时数据更新
        this.refreshData()
        
        // 更新图表数据
        this.chartData.times.shift()
        this.chartData.tvocData.shift()
        this.chartData.pm25Data.shift()
        
        const now = new Date()
        this.chartData.times.push(now.getHours().toString().padStart(2, '0'))
        this.chartData.tvocData.push((Math.random() * 2 + 0.5).toFixed(1))
        this.chartData.pm25Data.push(Math.floor(Math.random() * 30) + 10)
        
        this.updateChart()
      }, 8000) // 每8秒更新一次
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.device-selector {
  margin-bottom: 20px;
}

.selector-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.selector-item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.label {
  font-weight: bold;
  color: #303133;
}

.device-tabs {
  display: flex;
  gap: 20px;
}

.air-quality-cards {
  margin-bottom: 20px;
}

.air-quality-card {
  text-align: center;
  border-radius: 8px;
  color: white;
  margin-bottom: 16px;
}

.co2-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.o3-card {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.pm10-card {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.tvoc-card {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 10px;
}

.card-content .value {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 2px;
}

.card-content .unit {
  font-size: 12px;
  opacity: 0.8;
  margin-bottom: 5px;
}

.card-content .status {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.2);
}

.status.normal {
  background: rgba(103, 194, 58, 0.8);
}

.status.warning {
  background: rgba(230, 162, 60, 0.8);
}

.status.danger {
  background: rgba(245, 108, 108, 0.8);
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chart-legend {
  display: flex;
  gap: 20px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 14px;
}

.legend-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
}

.legend-item.tvoc .legend-dot {
  background-color: #67C23A;
}

.legend-item.pm25 .legend-dot {
  background-color: #E6A23C;
}

.status-indicators {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.status-item {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 10px;
  border-radius: 8px;
  background: #F5F7FA;
}

.status-circle {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
}

.co2-status {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.o3-status {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.pm10-status {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.tvoc-status {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.status-info {
  flex: 1;
}

.status-value {
  font-size: 16px;
  font-weight: bold;
  color: #303133;
}

.status-label {
  font-size: 14px;
  color: #909399;
  margin: 2px 0;
}

.status-level {
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 8px;
  display: inline-block;
}

.status-level.normal {
  background: #F0F9FF;
  color: #67C23A;
}

.status-level.warning {
  background: #FDF6EC;
  color: #E6A23C;
}

.status-level.danger {
  background: #FEF0F0;
  color: #F56C6C;
}

.log-content {
  max-height: 300px;
  overflow-y: auto;
}

.log-header {
  display: grid;
  grid-template-columns: 60px 120px 1fr;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid #EBEEF5;
  font-weight: bold;
  color: #303133;
}

.log-list {
  max-height: 250px;
  overflow-y: auto;
}

.log-item {
  display: grid;
  grid-template-columns: 60px 120px 1fr;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid #F5F7FA;
  font-size: 14px;
}

.log-index {
  color: #909399;
}

.log-time {
  color: #606266;
  font-size: 12px;
}

.log-content-text {
  color: #303133;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .selector-content {
    flex-direction: column;
    gap: 15px;
  }
  
  .air-quality-cards .el-col {
    margin-bottom: 10px;
  }
}
</style>